package com.jiuyou.storeController.common;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.StoreUser;
import com.ruoyi.common.core.domain.model.LoginStoreUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.StoreUserTokenService;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.store.service.IStoreUserRoleService;
import com.ruoyi.technician.domain.StoreTechnician;
import com.ruoyi.technician.service.IStoreTechnicianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author chengliang
 * @date 2021/11/24
 **/
@Component
public class StoreBaseController extends BaseController {
    @Autowired
    private StoreUserTokenService tokenService;
    @Autowired
    private IStoreTechnicianService technicianService;

    @Autowired
    private IStoreUserRoleService roleService;

    public StoreUserRole getStoreUserRole(Long storeId){
        if(null==storeId){
            storeId=parseStoreId();
        }
       /* List<StoreUserRole> storeUserRoles = roleService.selectStoreUserRoleByUserId(getLoginStoreUserId(),storeId ,null);
        if(storeUserRoles.size()>0){
            return storeUserRoles.get(0);
        }else{
           return null;
        }*/
        return getStoreUserRole(storeId,null);
    }

    public StoreUserRole getStoreUserRole(String roleType){
        return getStoreUserRole(null,roleType);
    }
    public StoreUserRole getStoreUserRole(){
       return getStoreUserRole(null,null);
    }
    public StoreUserRole getStoreUserRole(Long storeId,String roleType){
        if(null==storeId){
            storeId=parseStoreId();
        }
        List<StoreUserRole> storeUserRoles = roleService.selectStoreUserRoleByUserId(getLoginStoreUserId(),storeId ,roleType);
        if(storeUserRoles.size()>0){
            return storeUserRoles.get(0);
        }else{
            return null;
        }
    }

    /**
     * 获取当前所有权限汇总
     * @return
     */
    public List<String> getStoreUserRoleTypes(){
        Long storeId=parseStoreId();
        List<StoreUserRole> storeUserRoles = roleService.selectStoreUserRoleByUserId(getLoginStoreUserId(),storeId,null);
        List<String>  StoreUserRoleTypes= storeUserRoles.stream().map(StoreUserRole::getRoleType).distinct().collect(Collectors.toList());
        return StoreUserRoleTypes;
    }

    /**
     * 有roleType则返回roleType，没有则返回空值
     * @param roleType
     * @return
     */
    public StoreUserRole getDefultStoreUserRole(String ...roleType){
        Long storeId=parseStoreId();
        List<StoreUserRole> storeUserRoles = roleService.selectStoreUserRoleByUserId(getLoginStoreUserId(),storeId ,null);
        for (String s : roleType) {
            List<StoreUserRole> storeUserRolesCheck=storeUserRoles.stream().filter(r->s.equals(r.getRoleType())).collect(Collectors.toList());
            if(storeUserRolesCheck.size()>0){
                return storeUserRolesCheck.get(0);
            }
        }
        return null;
    }
    /**
     * 返回成功
     */
    public JSONObject returnSuccess(JSONObject jsonObject){
        jsonObject.put("success",true);
        jsonObject.put("msg","操作成功");
        return jsonObject;
    }
    /**
     * 返回失败
     */
    public JSONObject returnFail(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success",false);
        jsonObject.put("msg","操作失败");
        return jsonObject;
    }
    /**
     * 获取用户缓存信息
     */
    public LoginStoreUser getLoginStoreUser()
    {
        return tokenService.getLoginStoreUser();
    }
    /**
     * 获取用户缓存信息
     */
    public StoreUser getStoreUser()
    {

        return Optional.ofNullable(tokenService.getLoginStoreUser().getStoreUser()).orElseThrow(()->new CustomException("无法找到用户"));
    }

    /**
     * 获取登录用户id
     */
    public Long getLoginStoreUserId()
    {

        return  tokenService.getLoginStoreUserId();
    }
    /**
     * 获取门店Id
     */
    public Long parseStoreId()
    {
        try{
            return Optional.ofNullable(Long.parseLong(ServletUtils.getRequest().getHeader("StoreId"))).orElse(null);
        }catch (Exception e){
            return null;
        }
    }

    public LoginUser getLoginUser()
    {
        return null;
    }

    /**
     * 获取登录用户id
     */
    public Long getUserId()
    {

        return null;
    }

    /**
     * 获取登录部门id
     */
    public Long getDeptId()
    {
        return null;
    }

    /**
     * 获取登录用户名
     */
    public String getUsername()
    {
        return null;
    }


    /**
     * 获取当前登录的技师
     * @return
     */
    public StoreTechnician getStoreTechnician (){
        StoreUserRole role = getStoreUserRole(DicConstants.STORE_Identity_JiShi.getCode());
        if(role == null){
//            throw new CustomException("权限不足");
            return null;
        }
        List<String> list=getStoreUserRoleTypes();
        if( list.contains("store_jiShi")){
            Long storeUserRoleId=  role.getId();
            StoreTechnician storeTechnician=new StoreTechnician();
            storeTechnician.setUserRoleId(storeUserRoleId);
            List<StoreTechnician> storeTechnicianList=   technicianService.selectStoreTechnicianList(storeTechnician);
            if(storeTechnicianList.size()>0){
                return storeTechnicianList.get(0);
            }
        }else{
            throw new CustomException("权限不足");
        }
        return null;
    }
}
